package com.sunday.volunteer.ui.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.R;
import com.sunday.volunteer.adapter.InformationAdapter;
import com.sunday.volunteer.entity.Images;
import com.sunday.volunteer.http.HttpClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/1/12.
 */

public class InformationActivity extends BaseActivity{

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.swipfresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.title_text)
    TextView title_text;


    private int pageNo = 1;
    private boolean isCanloadMore = false;
    private LinearLayoutManager layoutManager;
    private InformationAdapter adapter;
    private List<Images> list;
    private List<Images> list2;
    private String code = "tdgs";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        ButterKnife.bind(this);
        title_text.setText("志愿者培训");

        list = new ArrayList<>();
        list2 = new ArrayList<>();
        adapter = new InformationAdapter(list ,list2);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        swipeRefreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .drawable(R.drawable.shape_divider)
                .build());
        recyclerView.setAdapter(adapter);

        getData();
        handlerRecyclerView();

    }


    private int lastVisibleItem;

    private void handlerRecyclerView() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                getData();
            }
        });
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                if (newState == RecyclerView.SCROLL_STATE_IDLE
//                        && lastVisibleItem + 3 == adapter.getItemCount() && isCanloadMore) {
//                    pageNo ++;
//                    getData();
//                }
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
//                isCanloadMore = dy>0?true:false;
//            }
//        });
    }

    private void getData() {

        Call<ResultDO<List<Images>>> call = HttpClient.getHttpAdapter().getNews("2");
        call.enqueue(new Callback<ResultDO<List<Images>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Images>>> call, Response<ResultDO<List<Images>>> response) {
                swipeRefreshLayout.setRefreshing(false);
                if(response.body() == null){
                    return;
                }
                if(response.body().getCode() == 0&&response.body().getResult() != null){
                    if(pageNo == 1){
                        list.clear();
                    }
                    list.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResultDO<List<Images>>> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
            }
        });

        Call<ResultDO<List<Images>>> call2 = HttpClient.getHttpAdapter().getNews("1");
        call2.enqueue(new Callback<ResultDO<List<Images>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Images>>> call, Response<ResultDO<List<Images>>> response) {
                if(response.body() == null){
                    return;
                }
                if(response.body().getCode() == 0&&response.body().getResult() != null){
                    if(pageNo == 1){
                        list2.clear();
                    }
                    list2.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResultDO<List<Images>>> call, Throwable t) {

            }
        });

    }

}
